/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ncsu.csc.usart;

import com.sun.spot.core.peripheral.TimeoutException;
import com.sun.spot.edemo.EDemoBoard;
import com.sun.spot.edemo.EDemoController;

import java.io.*;
/**
 *
 * @author Richard Pospesel
 */
public class USART
{
	//*
    private static EDemoBoard board = null;
	private static EDemoController controller = null; /*/
    private static StreamConnection conn = null; //*/

	private static long timeout = 50;
	private static InputStream in = null;
	private static OutputStream out = null;
    

	public static void initialize(int baud, int databits, int parity, int stopbits)
	{
		if (in != null) {
            try {
                in.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            in = null;
        }

		if (out != null) {
            try {
                out.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            out = null;
        }
        //*
        if(board == null)
		{
			board = EDemoBoard.getInstance();
			controller = board.getEDemoController();
			controller.initUART(baud, databits, parity, stopbits);
            in = new USARTInputStream();
			out = new USARTOutputStream();
		}
        /*/
        if (conn != null) {
            try {
                conn.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            conn = null;
        }
        String pariString="";
        switch (parity) {
            case EDemoBoard.SERIAL_PARITY_EVEN:
                pariString = "even";
                break;
            case EDemoBoard.SERIAL_PARITY_ODD:
                pariString = "odd";
                break;
            case EDemoBoard.SERIAL_PARITY_NONE:
                pariString = "none";
                break;
            default:
        }
        String url = "serial://usart?baudrate=" + baud + "&databits=" + databits + "&parity=" + pariString + "&stopbits=" + stopbits;
        try {
            System.out.println("Clearing out...");
            Isolate currentIsolate = Isolate.currentIsolate();
            currentIsolate.clearOut();
            currentIsolate.clearErr();
            currentIsolate.addOut("serial://usb"); // not required but
            currentIsolate.addErr("serial://usb"); //	useful for development 
            System.out.println("clear!");
            conn = (StreamConnection)Connector.open(url);
            System.out.println("Connection open");
            in = conn.openInputStream();
            out = conn.openOutputStream();
            System.out.println("Streams open");

        } catch (IOException ex) {
            ex.printStackTrace();
        } //*/
	}

	public static void setTimeout(long timeout)
	{
		USART.timeout = timeout;
	}

	public static InputStream getInputStream()
	{
		return in;
	}

	public static OutputStream getOutputStream()
	{
		return out;
	}
//*
	public static int readUART() throws IOException
	{
		try
		{
			return controller.readUART(timeout);
		}
		catch(TimeoutException t)
		{
                    System.out.println(t);
		}
		return -1;
	}

	public static int readUART(byte[] b) throws IOException
	{
		int bytes = b.length; 
		int count;
		for(count = 0; count < bytes; count++)
		{
			int read = readUART();
			if(read != -1)
				b[count] = (byte)read;
			else
				count--;
		}
		return bytes;
	}

	public static void writeUART(byte val)
	{
		controller.writeUART(val);
	}

    public static int availableBytes() throws IOException
    {
        return controller.availableUART();
    }//*/

}